﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace LingShi.Helpers
{
    /// <summary>
    /// ログポイント
    /// </summary>
    public enum LogPoint
    {
        CALL = 0,
        DONE = 1
    }
    /// <summary>
    /// ステータス
    /// </summary>
    public enum LogStatus
    {
        OK = 0,
        NG = 1
    }
    /// <summary>
    /// 実施操作
    /// </summary>
    public enum ActionType
    {
        ACCESS,
        CALL,
        CREATE,
        UPDATE,
        DELETE
    }
    /// <summary>
    /// 操作詳細 ACP:会議参加、コンテンツ、管理者
    /// </summary>
    public enum ACPDetailType
    {
        会議参加,
        複写予約,
        削除会議,
        会議変更,
        会議予約,
        コンテンツ,
        管理者
    }

    /// <summary>
    /// 操作詳細 LDAP:会社、本部、部、課、個人
    /// </summary>
    public enum LDAPDetailType
    {
        会社,
        本部,
        部,
        課,
        個人
    }

    public class LogInfo
    {
        /// <summary>
        /// ログ種別
        /// </summary>
        public string Kind { get; set; }
        /// <summary>
        /// ログ表示：日時
        /// </summary>
        public string DateTime { get; set; }
        /// <summary>
        /// ログ表示：操作
        /// </summary>
        public string LogAction { get; set; }
        /// <summary>
        /// ログ表示：操作詳細
        /// </summary>
        public string DetailAction { get; set; }
        /// <summary>
        /// ログ表示：実施操作
        /// </summary>
        public string ActionType { get; set; }
        /// <summary>
        /// ログ表示：操作者
        /// </summary>
        public string Operator { get; set; }
        /// <summary>
        /// ログ表示：ログポイント
        /// </summary>
        public string LogPoint { get; set; }
        /// <summary>
        /// ログ表示：ステータス
        /// </summary>
        public string Status { get; set; }
        /// <summary>
        /// ログ表示：理由
        /// </summary>
        public string Reason { get; set; }
    }

    public class LogHelper
    {
        /// <summary>
        /// ログファイルの保存のフォルダ
        /// </summary>
        public static string LogFileFolder = ConfigurationManager.AppSettings["LogFileFolder"];
        /// <summary>
        /// ログの周期(MONTH, WEEK, DAY)
        /// </summary>
        public static string LogCycle = ConfigurationManager.AppSettings["LogCycle"].ToUpper();
        /// <summary>
        /// ログの形式
        /// </summary>
        public static string LogFormat = "日時:{0}, 種別:{1}, 操作:{2},  操作詳細:{3},  実施操作:{4},  操作者:{5},  ログポイント:{6},  ステータス:{7},  理由:{8}";

        /// <summary>
        /// LDAPのログ(Start)
        /// </summary>
        /// <param name="loginfo"></param>
        public static void WriteLogForLDAP(LDAPDetailType detailType, ActionType actionType, string operatorID)
        {
            WriteLogForLDAP(detailType, actionType, operatorID, LogPoint.CALL, LogStatus.OK, string.Empty);
        }
        /// <summary>
        /// LDAPのログ
        /// </summary>
        /// <param name="loginfo"></param>
        public static void WriteLogForLDAP(LDAPDetailType detailType, ActionType actionType, string operatorID, LogPoint point, LogStatus status, string reason)
        {
            WriteLog("LDAP", "LDAPデータ取込", detailType.ToString(), operatorID, actionType.ToString(), point.ToString(), status.ToString(), reason);
        }
        /// <summary>
        /// ACPのログ(Start)
        /// </summary>
        /// <param name="acpDetailType"></param>
        /// <param name="operatorID"></param>
        public static void WriteLogForACP(ACPDetailType detailType, string operatorID)
        {
            WriteLogForACP(detailType, operatorID, ActionType.CALL, LogPoint.CALL, LogStatus.OK, string.Empty);
        }

        /// <summary>
        /// ACPのログ
        /// </summary>
        /// <param name="loginfo"></param>
        public static void WriteLogForACP(ACPDetailType detailType, string operatorID, ActionType actionType, LogPoint point, LogStatus status, string reason)
        {
            WriteLog("ACP", "ACP画面呼び出し", detailType.ToString(), operatorID, actionType.ToString(), point.ToString(), status.ToString(), reason);
        }

        /// <summary>
        /// LOGINのログ(Start)
        /// </summary>
        /// <param name="loginfo"></param>
        public static void WriteLogForLOGIN(string operatorID, ActionType actionType)
        {
            WriteLogForLOGIN(operatorID, actionType, LogPoint.CALL, LogStatus.OK, string.Empty);
        }
        /// <summary>
        /// LOGINのログ
        /// </summary>
        /// <param name="loginfo"></param>
        public static void WriteLogForLOGIN(string operatorID, ActionType actionType, LogPoint point, LogStatus status, string reason)
        {
            WriteLog("LOGIN", "LDAP認証", "LOGIN", operatorID, actionType.ToString(), point.ToString(), status.ToString(), reason);
        }

        /// <summary>
        /// WEBのログ(Start)
        /// </summary>
        /// <param name="loginfo"></param>
        public static void WriteLogForWEB(string webID, string operatorID, ActionType actionType)
        {
            WriteLogForWEB(webID, operatorID, actionType, LogPoint.CALL, LogStatus.OK, string.Empty);
        }
        /// <summary>
        /// WEBのログ
        /// </summary>
        /// <param name="loginfo"></param>
        public static void WriteLogForWEB(string webID, string operatorID, ActionType actionType, LogPoint point, LogStatus status, string reason)
        {
            WriteLog("WEB", "WEB予約画面操作", webID, operatorID, actionType.ToString(), point.ToString(), status.ToString(), reason);
        }

        /// <summary>
        /// 基本的なログの出力のメッソド
        /// </summary>
        /// <param name="detailAction"></param>
        /// <param name="operatorID"></param>
        /// <param name="actionType"></param>
        /// <param name="point"></param>
        /// <param name="status"></param>
        /// <param name="reason"></param>
        public static void WriteLog(string kind, string logAction, string detailAction, string operatorID, string actionType, string point, string status, string reason)
        {
            LogInfo loginfo = new LogInfo();

            loginfo.DateTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            loginfo.Kind = kind;
            loginfo.LogAction = logAction;
            loginfo.DetailAction = detailAction;
            loginfo.Operator = operatorID;
            loginfo.ActionType = actionType.ToString();
            loginfo.LogPoint = point.ToString();
            loginfo.Status = status.ToString();
            loginfo.Reason = reason;

            WriteLog(loginfo);
        }

        /// <summary>
        /// 基本的なログの出力のメッソド
        /// </summary>
        /// <param name="loginfo"></param>
        public static void WriteLog(LogInfo loginfo)
        {
            string fileName = GetLogFileName();

            //日時:{0}, 種別:{1}, 操作:{2},  操作詳細:{3},  実施操作:{4},  操作者:{5},  ログポイント:{6},  ステータス:{7},  理由:{8}
            string logString = string.Format(LogHelper.LogFormat
                                             , loginfo.DateTime
                                             , loginfo.Kind
                                             , loginfo.LogAction
                                             , loginfo.DetailAction
                                             , loginfo.ActionType
                                             , loginfo.Operator
                                             , loginfo.LogPoint
                                             , loginfo.Status
                                             , loginfo.Reason);

            System.Diagnostics.Debug.WriteLine("ログ:" + logString);

            using (StreamWriter write = File.AppendText(LogHelper.LogFileFolder + fileName))
            {
                write.WriteLine(logString);
            }
        }

        /// <summary>
        /// デイバグのため,エラーが発生したら、ログを出す
        /// </summary>
        /// <param name="message"></param>
        public static void WriteDebug(string message)
        {
            string fileName = GetLogFileName();

            System.Diagnostics.Debug.WriteLine("エラー:" + message);
            using (StreamWriter write = File.AppendText(LogHelper.LogFileFolder + "Debug\\" + fileName))
            {
                write.WriteLine(message);
            }
        }
        /// <summary>
        /// ログファイルの名を取得する。
        /// </summary>
        /// <returns></returns>
        public static string GetLogFileName()
        {
            string fileName = "";

            if (LogCycle == "MONTH")
            {
                //fileName = DateTime.Today.ToString("yyyy-MM") + ".log";
                fileName = DateTime.Today.ToString("yyyyMM") + ".log";
            }
            else if (LogCycle == "WEEK")
            {
                DateTime monday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek);
            }
            else if (LogCycle == "DAY")
            {
                //fileName = DateTime.Today.ToString("yyyy-MM-dd") + ".log";
                fileName = DateTime.Today.ToString("yyyyMMdd") + ".log";
            }

            return fileName;
        }
    }
}